package com.ruoyi.product.Service.impl;

import com.ruoyi.product.Service.ProductService;
import com.ruoyi.product.controller.ActivitiController;
import com.ruoyi.product.mapper.ProductMapper;
import com.ruoyi.product.mapper.UesrMapper;
import com.ruoyi.product.pojo.Product;
import com.ruoyi.product.pojo.Resuit;
import com.ruoyi.product.pojo.Uesr;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

	@Resource
	ProductMapper productMapper;

	@Resource
	UesrMapper uesrMapper;


	@Override
	public List<Product> selectByProduct(Product product) {
		return productMapper.selectByProduct(product);
	}

	@Override
	public void updateById(Product product) {
		productMapper.updateById(product);
	}

	@Override
	public void insertProduct(Product product) {
		product.setDeadline(new Date());
		ActivitiController.deploy();
		String pid = ActivitiController.testStartProcessAndSetVariables("航子");
		product.setPid(Integer.valueOf(pid));
		product.setState("未上线");
		this.fulfilaTask(Integer.valueOf(pid));
		productMapper.insertProduct(product);
	}

	@Override
	public void deleteById(long pid) {
		productMapper.deleteById(pid);
	}

	//查询
	@Override
	public Resuit queryTask(int uid) {
		Uesr uesr =uesrMapper.selectById(uid);
		List<Product> products = new ArrayList<>();
		List<String> strings = null;
		try {
			if (uesr.getJurisdiction().equals("用户")){
				strings = ActivitiController.testFindPersonalTaskList(uesr.getUname());
			}else{
				strings = ActivitiController.testFindPersonalTaskList(uesr.getJurisdiction());
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		for (String string : strings) {
			 Product product=productMapper.selectById(Integer.valueOf(string));
			 products.add(product);
		}
		return new Resuit(200,"查询成功",products);
	}


	/**
	 * 完成任务
	 * @param pid
	 * @return
	 */
	@Override
	public Resuit fulfilaTask(Integer pid) {
		String s = ActivitiController.completTaskByVariables(String.valueOf(pid));
		if(s.equals("经理")){
			Product product = new Product();
			product.setPid(pid);
			product.setState("上线");
			productMapper.updateByState(product);
		}
		return new Resuit(200,"",s);
	}
}
